//
//  YJCycleProgressButton.m
//  cycleViewTest
//
//  Created by crj on 2017/8/15.
//  Copyright © 2017年 crj. All rights reserved.
//

#import "YJCycleProgressButton.h"

@interface YJCycleProgressButton()

@property (nonatomic, strong) CAShapeLayer *backgroundLine;
@property (nonatomic, strong) CAShapeLayer *progressLine;

@end

@implementation YJCycleProgressButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setConfig];
        [self.layer addSublayer:self.backgroundLine];
        [self.layer addSublayer:self.progressLine];
    }
    return self;
}

- (void)setConfig{
    _strokeLineWidth = 5;
    _backLineColor = [UIColor lightGrayColor];
    _progressLineColor = [UIColor blueColor];
    
}

- (void)updateProgress:(CGFloat)progress{
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) radius:self.bounds.size.width/2 - _strokeLineWidth/2 startAngle:M_PI_2 endAngle: M_PI + M_PI*2 clockwise:YES];
    
    _backgroundLine.path = path.CGPath;

    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) radius:self.bounds.size.width/2-_strokeLineWidth/2 startAngle:-M_PI_2 endAngle:-M_PI_2 + M_PI*2*progress clockwise:YES];
    self.progressLine.path = path2.CGPath;

}

- (void)layoutSubviews{

    [super layoutSubviews];
    _backgroundLine.frame = self.bounds;
    _progressLine.frame = self.bounds;
}

#pragma mark - Get

- (CAShapeLayer *)backgroundLine {
    
    if(_backgroundLine == nil) {
        _backgroundLine = [[CAShapeLayer alloc] init];
        _backgroundLine.fillColor = [UIColor clearColor].CGColor;
        _backgroundLine.strokeColor = _backLineColor.CGColor;
        _backgroundLine.lineWidth = _strokeLineWidth;
        _backgroundLine.backgroundColor = [UIColor whiteColor].CGColor;
    }
    return _backgroundLine;
}

- (CAShapeLayer *)progressLine {
    if(_progressLine == nil) {
        _progressLine = [[CAShapeLayer alloc] init];
        _progressLine.fillColor = [UIColor clearColor].CGColor;
        _progressLine.strokeColor = _progressLineColor.CGColor;
        _progressLine.lineWidth = _strokeLineWidth;
    }
    return _progressLine;
}


#pragma mark - Set

- (void)setBackLineColor:(UIColor *)backLineColor{

    _backLineColor = backLineColor;
    _backgroundLine.strokeColor = _backLineColor.CGColor;
}

- (void)setProgressLineColor:(UIColor *)progressLineColor{

    _progressLineColor = progressLineColor;
    _progressLine.strokeColor = _progressLineColor.CGColor;
}
- (void)setStrokeLineWidth:(CGFloat)strokeLineWidth{

    _strokeLineWidth = strokeLineWidth;
    _backgroundLine.lineWidth = _strokeLineWidth;
    _progressLine.lineWidth = _strokeLineWidth;
}


@end
